<template>
  <CRUD
    v-bind="$props"
    :onSelect="onSelect"
    :onUpdate="onUpdate"
    :onCreate="onCreate"
    :onDelete="onDelete"
  ></CRUD>
</template>

<script>
import axios from "../axios/AxiosConfig";
import CRUD from "components/CRUD.vue";

export default {
  components: { CRUD },
  props: {
    entity: {
      type: String,
      default: "" // User, Role, Group, ... 实体名
    },
    populate: {
      type: Array,
      default: () => [] // 关联查询
    },
    isGrid: {
      type: Boolean,
      default: false
    },
    showSearchBox: {
      type: Boolean,
      default: true
    },
    isCreateBtn: {
      type: Boolean,
      default: true
    },
    showButtons: {
      type: Boolean,
      default: true
    },
    title: {
      type: String,
      default: ""
    },
    columns: {
      type: Array,
      default: () => []
    },
    form: {
      type: Object,
      default: () => ({})
    }
  },
  data() {
    return {};
  },
  methods: {
    async onSelect(p) {
      const params = p;
      if (this.populate.length) {
        params.populate = this.populate.join(",");
      }
      return await axios.request({
        method: "POST",
        url: "/crud",
        params,
        data: {
          entity: this.entity,
          method: "find",
          data: {}
        }
      });
    },
    async onUpdate(id, data) {
      return await axios.request({
        method: "POST",
        url: "/crud",
        params: { where: `id:${id}` },
        data: {
          entity: this.entity,
          method: "update",
          data: data
        }
      });
    },
    async onCreate(data) {
      return await axios.request({
        method: "POST",
        url: "/crud",
        data: {
          entity: this.entity,
          method: "create",
          data: data
        }
      });
    },
    async onDelete(id) {
      return await axios.request({
        method: "POST",
        url: "/crud",
        params: { where: `id:${id}` },
        data: {
          entity: this.entity,
          method: "delete"
        }
      });
    }
  }
};
</script>

<style></style>
